package cn.le.thread;

/**
 *所有的线程都隶属一个线程组，可以在创建时制定组，如果不制定组，那就属于系统组。
 *线程组中线程可以修改组内及下属组内的线程
 *
 */
public class TestThreadGroup {

    public static void main(String[] args) {

        ThreadGroup
                x = new ThreadGroup("x"),
                y = new ThreadGroup(x,"y"),
                z = new ThreadGroup(y,"z");
        Thread one = new TestThread1(x,"one"),
                two = new TestThread2(z,"two");

    }
}
class TestThread1 extends Thread {

    private int i;
    TestThread1(ThreadGroup g,String name)
    {
        super(g,name);
    }
    void f()
    {
        i++;
        System.out.println(getName()+" f()");
    }
}
class TestThread2 extends TestThread1{

    TestThread2(ThreadGroup g, String name){
        super(g,name);
        start();;
    }
    public void run()
    {
        ThreadGroup g = getThreadGroup()
                //返回此线程的父线程
                .getParent()
                .getParent();
        // 将有关此线程组的信息打印到标准输出。
        g.list();
        //g.activeCount() 返回此线程组中活动的估计数
        Thread[] gAll = new Thread[g.activeCount()];
        // 把此线程组及其子组中的所有活动线程复制到指定数组中
        g.enumerate(gAll);
        for (int i = 0; i < gAll.length; i++) {
            gAll[i].setPriority(Thread.MIN_PRIORITY);
            ((TestThread1)gAll[i]).f();
        }
    }


}